var express = require("express");
var router = express.Router();
var db = require("../db");
const { request } = require("express");


router.all('*', function(req, res, next) {
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Headers", "X-Requested-With");
    res.header("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS");
    res.header("X-Powered-By", ' 3.2.1')
    res.header("Content-Type", "application/json;charset=utf-8");
    next();
});
/* GET home page. */
router.get("/", function(req, res, next) {
    res.render("index", { title: "Express" });
});
// 个人注册
router.all("/userzhuce", function(req, res, next) {
    var zhanghao = req.body.zhanghao || req.query.zhanghao;
    var password = req.body.password || req.query.password;
    var email = req.body.email || req.query.email;
    if (zhanghao == '' || password == '' || email == '' || zhanghao == undefined || password == undefined || email == undefined) {
        res.send({ msg: "信息不能为空", status: 1 });
    } else {
        console.log(zhanghao, password, email)
        db.query("select * from user where zhanghao=?", [zhanghao], function(
            results,
            fields
        ) {
            if (results.length) {
                res.send({ msg: "用户名已存在", status: 1 });
            } else {
                db.query(
                    "insert into user(zhanghao,password,email) values(?,?,?)", [zhanghao, password, email],
                    function(results, fields) {
                        if (fields) {
                            throw fields;
                            return;
                        }
                        res.send({ msg: "注册成功", status: 0 });
                    }
                );
            }
        });
    }
});
// 个人登录接口
router.post("/userlogin", function(req, res, next) {
    var zhanghao = req.body.zhanghao;
    var password = req.body.password;
    if (zhanghao == '' || password == '' || zhanghao == undefined || password == undefined) {
        res.send({ msg: "KEY之不能为空", status: 400 });
    } else {
        db.query("select * from user where zhanghao=?", [zhanghao], function(
            results,
            fields
        ) {
            console.log(results[0]);
            if (results.length) {
                if (results[0].password == password) {
                    res.send({ msg: "登录成功" });
                } else {
                    res.send({ msg: "用户名或密码有误" });
                }
            } else {
                res.send({ msg: "用户名不存在" });
            }
        });
    }

});

// hr注册
router.post("/hrzhuce", function(req, res, next) {
    var zhanghao = req.body.zhanghao;
    var password = req.body.password;
    var email = req.body.email;
    if (zhanghao == '' || password == '' || email == '' || zhanghao == undefined || password == undefined || email == undefined) {
        res.send({ msg: "KEY值不能为空", status: 1 });
    } else {
        db.query("select * from hr where zhanghao=?", [zhanghao], function(
            results,
            fields
        ) {
            if (results.length) {
                res.send({ msg: "用户名已存在", status: 1 });
            } else {
                db.query(
                    "insert into hr(zhanghao,password,email) values(?,?,?)", [zhanghao, password, email],
                    function(results, fields) {
                        if (fields) {
                            throw fields;
                            return;
                        }
                        res.send({ msg: "注册成功", status: 0 });
                    }
                );
            }
        });
    }
});
// hr登录接口
router.post("/hrlogin", function(req, res, next) {
    var zhanghao = req.body.zhanghao;
    var password = req.body.password;
    if (zhanghao == '' || password == '' || zhanghao == undefined || password == undefined) {
        res.send({ msg: "KEY之不能为空", status: 400 });
    } else {
        db.query("select * from hr where zhanghao=?", [zhanghao], function(
            results,
            fields
        ) {
            console.log(results[0]);
            if (results.length) {
                if (results[0].password == password) {
                    res.send({ msg: "登录成功" });
                } else {
                    res.send({ msg: "用户名或密码有误" });
                }
            } else {
                res.send({ msg: "用户名不存在" });
            }
        });
    }
});
// 创建文章
router.post("/createnote", function(req, res, next) {
    var openid = req.body.openid;
    var content = req.body.content;
    var time = req.body.time;
    var type = req.body.type;
    if (openid == '' || content == '' || time == '' || openid == undefined || content == undefined || time == undefined) {
        res.send({ msg: "KEY之不能为空", status: 400 });
    } else {
        db.query(
            "insert into community(openid,content,time,type) values(?,?,?,?)", [openid, content, time, type],
            function(results, fields) {
                if (fields) {
                    throw fields;
                    return;
                }
                res.send({ msg: "添加成功" });
            }
        );
    }
});
// 文章详情接口
router.post("/notexiangqing", function(req, res, next) {
    var id = req.body.id;
    db.query("select * from community where id=?", [id], function(
        results,
        fields
    ) {
        res.send(results);
    });
});
// 个人文章列表
router.post("/notelist", function(req, res, next) {
    var openid = req.body.openid;
    db.query("select * from userinfo,community where userinfo.openid=community.openid AND userinfo.openid=?", [openid], function(
        results,
        fields
    ) {
        res.send(results);
    });
});
// 所有文章列表
router.post("/allnotelist", function(req, res, next) {
    db.query("select * from userinfo,community where userinfo.openid=community.openid", [], function(results, fields) {
        res.send(results);
    });
});
//发布职位需求接口
router.post("/jobs", function(req, res, next) {
    var job = req.body.job;
    var pay = req.body.pay;
    var xuqiu = req.body.xuqiu;
    var xueli = req.body.xueli;
    var jingyan = req.body.jingyan;
    var gongsi = req.body.gongsi;
    if (job == '' || pay == '' || xueli == '' || jingyan == '' || gongsi == '' || job == undefined || pay == undefined || xueli == undefined || jingyan == undefined || gongsi == undefined) {
        res.send({ msg: "KEY之不能为空", status: 400 });
    } else {
        db.query(
            "insert into jobs(job,pay,xuqiu,jingyan,xueli,gongsi) values(?,?,?,?,?,?)", [job, pay, xuqiu, xueli, jingyan, gongsi],
            function(results, fields) {
                if (fields) {
                    throw fields;
                    return;
                }
                res.send({ msg: "发布成功" });
            }
        );
    }
});
//搜索职位接口
router.post("/selectjobs", function(req, res, next) {
    var job = "%" + req.body.job + "%";
    db.query("select * from jobs where job like ?", [job], function(results, fields) {
        res.send(results);
    });
});
//查看职位接口
router.post("/searchjobs", function(req, res, next) {
    db.query("select * from jobs", [], function(results, fields) {
        res.send(results);
    });
});
//职位详情接口
router.post("/jobsdetail", function(req, res, next) {
    var id = req.body.id;
    db.query("select * from jobs where id=?", [id], function(results, fields) {
        res.send(results);
    });
});
//提交建议
router.post("/yijian", function(req, res, next) {
    var yijian = req.body.yijian;
    var time = req.body.time;
    db.query(
        "insert into yijian(yijian,time) values(?,?)", [yijian, time],
        function(results, fields) {
            if (fields) {
                throw fields;
                return;
            }
            res.send({ msg: "感谢您的反馈！" });
        }
    );
});

//个人资料
router.post("/grzl", function(req, res, next) {
    console.log(req.body);
    var openid = req.body.openid; //用户名
    var name = req.body.name; //姓名
    var sex = req.body.sex; //性别
    var age = req.body.age; //年龄
    var school = req.body.school; //学校
    var major = req.body.major; //专业
    var exp = req.body.exp; //经验
    if (name == '' || sex == '' || age == '' || school == '' || major == '' || exp == '' || exp == undefined || name == undefined || sex == undefined || age == undefined || school == undefined || major == undefined) {
        res.send({ msg: "KEY之不能为空", status: 400 });
    } else {
        db.query("select * from userinfo where openid=?", [openid], function(
            results,
            fields
        ) {
            if (results.length) {
                var modSql =
                    "UPDATE userinfo SET name=?,sex=?,age=?,school=?,major=?,exp=? WHERE openid=?";
                var modSqlParams = [openid, name, sex, age, school, major, exp];
                db.query(modSql, modSqlParams, function(err, result) {
                    if (err) {
                        console.log(err.message);
                        return;
                    }
                    res.send({ msg: "更新成功", status: 1 });
                });
            } else {
                var addSql =
                    "INSERT INTO userinfo (openid,name,sex,age,school,major,exp) VALUES(?,?,?,?,?,?,?)";
                var addSqlParams = [openid, name, sex, age, school, major, exp];
                db.query(addSql, addSqlParams, function(results, fields) {
                    if (fields) {
                        throw fields;
                        return;
                    }
                    res.send({ msg: "添加成功", status: 1 });
                });
            }
        });
    }
});
//用户个人中心
router.post("/getuserinfo", function(req, res, next) {
    var openid = req.body.openid;
    db.query("select * from userinfo where openid=?", [openid], function(results, fields) {
        res.send(results);
    });
});
//企业资料
router.post("/qyzl", function(req, res, next) {
    console.log(req.body);
    var zhanghao = req.body.zhanghao; //用户名
    var company = req.body.company; //公司名称
    var address = req.body.address; //地址
    var position = req.body.position; //
    var tel = req.body.tel; //电话
    var name = req.body.name; //
    if (zhanghao == '' || company == '' || address == '' || position == '' || tel == '' || name == '' || name == undefined || zhanghao == undefined || company == undefined || address == undefined || position == undefined || tel == undefined) {
        res.send({ msg: "KEY之不能为空", status: 400 });
    } else {
        db.query("select * from hrinfo where zhanghao=?", [zhanghao], function(
            results,
            fields
        ) {
            if (results.length) {
                var modSql =
                    "UPDATE hrinfo SET company=?,address=?,position=?,tel=?,name=? WHERE zhanghao=?";
                var modSqlParams = [company, address, position, tel, name, zhanghao];
                db.query(modSql, modSqlParams, function(err, result) {
                    if (err) {
                        console.log(err.message);
                        return;
                    }
                    res.send({ msg: "更新成功", status: 1 });
                });
            } else {
                var addSql =
                    "INSERT INTO hrinfo (zhanghao,company,address,position,tel,name) VALUES(?,?,?,?,?,?)";
                var addSqlParams = [zhanghao, company, address, position, tel, name];
                db.query(addSql, addSqlParams, function(results, fields) {
                    if (fields) {
                        throw fields;
                        return;
                    }
                    res.send({ msg: "添加成功", status: 1 });
                });
            }
        });
    }
});
//企业个人中心
router.post("/searchjobs", function(req, res, next) {
    var zhanghao = req.body.zhanghao;
    db.query("select * from jobs where zhanghao ?", [zhanghao], function(results, fields) {
        res.send(results);
    });
});
//openid
router.get("/getopenid", function(req, res, next) {
    var JSCODE = req.query.code;
    var request = require('request');
    request('https://api.weixin.qq.com/sns/jscode2session?appid=wxba2a0788b79a0be9&secret=ca4444c7d2e32e4101c18083b9284afc&grant_type=authorization_code&js_code=' + JSCODE, function(err, response, body) {
        //err 当前接口请求错误信息
        //response 一般使用statusCode来获取接口的http的执行状态
        //body 当前接口response返回的具体数据 返回的是一个jsonString类型的数据 
        //需要通过JSON.parse(body)来转换
        if (!err && response.statusCode == 200) {
            //todoJSON.parse(body)
            var ress = JSON.parse(body);
            //  console.log(body)
            //  console.log(res)
            res.send(ress)
        }
    })
});
// //多表联查
// router.all("/useropenid", function(req, res, next) {
//     var openid = req.body.openid;
//     db.query("select * from userinfo,community where userinfo.openid=community.openid=?", [openid], function(results, fields) {
//         res.send(results);
//     });
// });
module.exports = router;